<?php
/**
 * @Author: laoweizhen <1149243551@qq.com>,
 * @Date: 2021/10/27 18:34,
 * @LastEditTime: 2021/10/27 18:34
 */

namespace Lwz\LaravelExtend\MQ\Services;


use Lwz\LaravelExtend\MQ\Enum\MQStatusLogEnum;
use Lwz\LaravelExtend\MQ\Interfaces\MQErrorLogServiceInterface;
use Lwz\LaravelExtend\MQ\Models\MQErrorLog;
use Lwz\LaravelExtend\MQ\Repositories\MQErrorLogRepository;
use Lwz\LaravelExtend\MQ\Library\MQHelper;

class MQErrorLogService implements MQErrorLogServiceInterface
{
    /**
     * 添加
     * @param string $mqUuid mq唯一标识
     * @param array|string $payload 负载，消息体
     * @param array $mqConfig mq配置
     * @param string $errMsg 错误信息
     * @param int $retryNum 重试次数
     * @param int $retryStatus 重试状态
     * @return mixed
     */
    public function addData(string $mqUuid, $payload, array $mqConfig, string $errMsg, int $retryNum = 0, int $retryStatus = MQStatusLogEnum::ERS_WAIT): MQErrorLog
    {
        return MQErrorLogRepository::add([
            'mq_uuid' => $mqUuid,
            'mq_config' => MQHelper::encodeData($mqConfig),
            'retry_num' => $retryNum,
            'retry_status' => $retryStatus,
            'payload' => is_string($payload) ? $payload : MQHelper::encodeData($payload),
            'error_msg' => $errMsg,
        ]);
    }

    /**
     * 通过 uuid 更新消息
     * @param string $mqUuid mq唯一标识（多个传数组）
     * @param array $data 更新的数据
     * @return mixed
     * @auth lwz
     */
    public function updateByMQUuid(string $mqUuid, array $data)
    {
        return MQErrorLogRepository::updateByWhere(['mq_uuid' => $mqUuid], $data);
    }

    /**
     * 获取详情
     * @param string $mqUuid
     * @return MQErrorLog|null
     * @author lwz
     */
    public function getDetailByMquuid(string $mqUuid): ?MQErrorLog
    {
        return MQErrorLogRepository::getOneByWhere(['mq_uuid' => $mqUuid]);
    }

    /**
     * 通过 uuid 删除消息
     * @param string|array $mqUuid mq唯一标识（多个传数组）
     * @return mixed
     * @auth lwz
     */
    public function deleteByMQUuid($mqUuid)
    {
        return MQErrorLogRepository::deleteByMQUuid($mqUuid);
    }
}
